package com.example.flowermall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flowermall.entity.CreateOrder;
import com.example.flowermall.entity.OrderItem;
import com.example.flowermall.entity.Orders;
import com.example.flowermall.entity.Result;
import com.example.flowermall.service.IOrderItemService;
import com.example.flowermall.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoman
 * @since 2023-09-06
 */
@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    IOrderService orderService;
    @Autowired
    IOrderItemService orderItemService;
    @GetMapping("/list")
    public Result list(@RequestParam(value = "pageNo",required = false)Long pageNo,
                       @RequestParam(value = "pageSize",required = false)Long pageSize){
        Page<Orders> page=new Page<>(pageNo,pageSize);
        orderService.page(page);
        Map<String,Object> data=new HashMap<>();
        List<Orders> records = page.getRecords();
        for(Orders orders:records){
            List<OrderItem> byOrderId = orderItemService.getByOrderId(orders.getOrderId());
            orders.setOrderItem(byOrderId);
        }
        Result result=Result.success();
        data.put("total",page.getTotal());
        data.put("rows",records);
        result.put("data",data);
        return result;
    }
    @GetMapping("/orderLists")
    public Result selectOrder(@RequestParam(value = "orderId",required = false)String orderId){
        Orders orders = new Orders();
        orders.setOrderId(orderId);
        List<Orders> select = orderService.select(orders);
        Result result=Result.success();
        result.put("data",select);
        return result;
    }
    @PostMapping("/add")
    public Result insertOrder(@RequestBody Orders orders){
        orderService.insertOrder(orders);
        return  Result.success("添加成功");
    }
    @GetMapping("/{id}")
    public Result getOrderById(@PathVariable("id")Integer id){
        Orders orders = orderService.getOrderById(id);
        Result result=Result.success();
        result.put("data", orders);
        return result;
    }
    @PutMapping
    public Result updateOrder(@RequestBody Orders orders){
        orderService.updateOrder(orders);
        return Result.success("修改成功");
    }
    @DeleteMapping("/delete/{orderId}")
    public Result deleteOrder(@PathVariable("orderId") String orderId){
        orderService.deleteOrder(orderId);
        return Result.success("删除成功");
    }

    @PutMapping("/confirm/{orderId}")
    public Result updateStatus(@PathVariable("orderId") String orderId){
        orderService.confirmOrder(orderId);
        return Result.success("修改成功");
    }

    @GetMapping("/orderList")
    public Result getListByUserId(@RequestParam(value ="userId")Integer userId,@RequestParam(value = "orderStatus")String orderStatus){
        return Result.success(orderService.getListByUserId(userId,orderStatus));
    }
    @GetMapping("/orderId")
    public Result getByOrderId(@RequestParam(value = "orderId")String orderId){
        Orders byOrderId = orderService.getByOrderId(orderId,0);
        Result result = Result.success();
        result.put("data",byOrderId);
        return result;
    }

    @PostMapping("/create")
    public Result createOrder(@RequestBody CreateOrder createOrder){
        Orders order = orderService.createOrder(createOrder);
        if(StringUtils.isEmpty(order)){
            return Result.error("创建订单失败");

        }
        return Result.success(order);
    }

    @GetMapping("/count/{userId}")
    public Result getCount(@PathVariable("userId")Integer userId){
        Map<String, Integer> count = orderService.getCount(userId);
        return Result.success(count);
    }

}
